package org.demo.web.controller.person;

import java.util.Date;
import java.util.List;

import org.demo.domain.person.Person;
import org.demo.domain.response.ResponseEnvelope;
import org.demo.domain.response.ResponseHeader;
import org.demo.facade.service.PersonFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping ("/private/person")
public class PersonResourceController {

	/* ---- Instance Variables ---- */

	@Autowired
	private PersonFacade personFacade = null;

	/* ---- Constructors ---- */

	/* ---- Rest Methods ---- */

	/**
	 * Ping service, mainly used to see if a connection to the UPay server can be made. The
	 * service returns the given message.
	 *
	 * @param message
	 * @return
	 */
	@RequestMapping ("/findall")
	public @ResponseBody ResponseEnvelope<List<Person>> findAllGET() {

		Long startTime = (new Date()).getTime();
		List<Person> persons = personFacade.findAll();
		Long stopTime =  (new Date()).getTime();

		ResponseEnvelope<List<Person>> envelope = new ResponseEnvelope<List<Person>>();
		ResponseHeader header = new ResponseHeader(null, Boolean.TRUE, stopTime - startTime);
		envelope.setResponseHeader(header);
		org.demo.domain.response.ResponseBody<List<Person>> body = new org.demo.domain.response.ResponseBody<List<Person>>(persons, null);
		envelope.setResponseBody(body);
		return envelope;
	}

	@RequestMapping ("/get")
	public @ResponseBody Person getPerson() {
		Person person = personFacade.get(1);
		return person;
	}

	@RequestMapping (value = "/find", method = RequestMethod.GET)
	public @ResponseBody List<Person> findPersons() {
		return personFacade.findAll();
	}

	@RequestMapping (value = "/insert", method = RequestMethod.POST)
	public void insertPerson(@RequestBody Person person) {
		System.out.println(person);
	}

	@RequestMapping (value = "/insertAll", method = RequestMethod.POST)
	public void insertPerson(@RequestBody List<Person> persons) {
		for (Person person : persons) {
			System.out.println(person);
		}
	}

	/* ---- Getters and Setters ---- */

}
